package edu.colorado.karl.manctrl;

import java.util.ArrayList;

import edu.colorado.karl.databases.SPResponse;
import edu.colorado.karl.databases.SetpointsDatabase;

/**
 * This class checks and modifies temperature setpoints.
 * It reads and writes the 'setpoints' table in the 'Setpoints' database.
 * @author Addison LeMessurier
 * @version 3/2/08
 */

public class ManualControl {
	private static ManualControl ref;	//only allowable reference to this class
	private static SetpointsDatabase spDB;
	
	/**
	* returns either an error or success message as a string
	* NOTE: always call sqlConnect() before this function and sqlDisconnect()
	* when done
	*/
	
	/**
	 * This function instantiates the singleton object for this class.
	 * @return a globally shared instance of ManualControl.
	 */
	public static ManualControl getManualControl() {
		if (ref == null) {
			ref = new ManualControl();
		}

		return ref;
	}

	/**
	 * This function returns the value of the specified set point.
	 * @param pointName The name of the set point to examine.
	 * @return the value of the set point.
	 */
	public static int getSetpoint(String pointName) {
		ArrayList<SPResponse> setpointList;
		
		if(pointName.equals(SetpointsDatabase.ROOM_RANGE) || 
				pointName.equals(SetpointsDatabase.ROOM_TEMP)) {
			setpointList = spDB.getRoomSetPoints();
		} else {
			setpointList = spDB.getTankSetPoints();
		}
		
		for (SPResponse response : setpointList) {
			if(response.getName().equals(pointName)) {
				return response.getValue();
			}
		}
		
		return -2;	
	}

	/**
	 * Returns the automatic/manual tank mode
	 * @return <code>True</code> if tank mode is in manual mode, 
	 * <code>false</code> if automatic
	 */
	
	public static boolean getTankMode() {
		return spDB.getMode();
	}
	
	/**
	 * This function changes the temperature set point for the house.
	 * @param setting The temperature set point.
	 * @param range The temperature set point differential.
	 */
	public static void updateRoomSettings(int setting, int range) {
		spDB.updateSetPoint(SetpointsDatabase.ROOM_TEMP, setting);
		spDB.updateSetPoint(SetpointsDatabase.ROOM_RANGE, range);
	}
	

	/**
	 * Updates the set point database for the tanks.
	 * @param hotSetting The hot tank set point
	 * @param hotRange The range around the hot tank to to reach
	 * @param coolSetting The cold tank set point
	 * @param coolRange The range around the cold tank set point to reach
	 */
	public static void updateTankSettings(int hotSetting, int hotRange,
			int coolSetting, int coolRange) {
		spDB.updateSetPoint(SetpointsDatabase.COLD_TANK_RANGE, coolRange);
		spDB.updateSetPoint(SetpointsDatabase.COLD_TANK_SP, coolSetting);
		spDB.updateSetPoint(SetpointsDatabase.HOT_TANK_RANGE, hotRange);
		spDB.updateSetPoint(SetpointsDatabase.HOT_TANK_SP, hotSetting);
	}
	
	/**
	 * 
	 * Updates the set point database for the tanks.
	 * @param hotSetting The hot tank set point
	 * @param hotRange The range around the hot tank to to reach
	 * @param coolSetting The cold tank set point
	 * @param coolRange The range around the cold tank set point to reach
	 * @param tankMode <code>True</code> if tank mode is in manual mode, 
	 * <code>false</code> if automatic
	 * @param heatingCoolingMode <code>True</code> if tank is heating, 
	 * <code>false</code> if cooling
	 */
	public static void updateTankSettings(int hotSetting, int hotRange,
			int coolSetting, int coolRange, boolean tankMode, 
			boolean heatingCoolingMode) {
		spDB.updateSetPoint(SetpointsDatabase.COLD_TANK_RANGE, coolRange);
		spDB.updateSetPoint(SetpointsDatabase.COLD_TANK_SP, coolSetting);
		spDB.updateSetPoint(SetpointsDatabase.HOT_TANK_RANGE, hotRange);
		spDB.updateSetPoint(SetpointsDatabase.HOT_TANK_SP, hotSetting);
		spDB.updateMode(SetpointsDatabase.MODE, tankMode);
		spDB.updateMode(SetpointsDatabase.HEATING_MODE, heatingCoolingMode);
	}
	
	/**
	 * This function checks whether the house is currently heating or cooling.
	 * @return heatingCoolingMode <code>True</code> if tank is heating, 
	 * <code>false</code> if cooling
	 */
	public static boolean getHeatingCoolingMode()
	{
		return spDB.getHeatingMode();
	}

	private ManualControl() {
		spDB = new SetpointsDatabase();
	}
}
